﻿Imports System.Data.OleDb

Public Class TagSubscribersManagement
    Inherits ConnectionClass

    Public Sub InsertTagSubscriber(ByVal UserId As Int32, ByVal TagSubscriberDate As DateTime, ByVal TagId As Int32)
        Try

            myConnection.Open()
            Dim SqlString As String = "INSERT INTO TagsSubscribers(UserId,TagSubscriberDate,TagId) VALUES(@f1,@f2,@f3)"
            Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
            newCmd.CommandType = CommandType.Text
            newCmd.Parameters.AddWithValue("@f1", UserId)
            newCmd.Parameters.AddWithValue("@f2", TagSubscriberDate)
            newCmd.Parameters.AddWithValue("@f3", TagId)
            newCmd.ExecuteNonQuery()
            myConnection.Close()

        Catch ex As Exception

        End Try
    End Sub

    Public Function SelectCountTagsByTagIdUserId(ByVal TagId As Int32, ByVal UserId As Int32) As Int32
        Try

            myConnection.Open()
            Dim Cmd As OleDbCommand = New OleDbCommand("Select Count(TagSubscriberId) from TagsSubscribers where UserId = @f1 and TagId = @f2 Group By TagSubscriberId ", myConnection)
            Cmd.Parameters.AddWithValue("@f1", UserId)
            Cmd.Parameters.AddWithValue("@f2", TagId)
            Dim dr = Cmd.ExecuteReader()
            If (dr.HasRows) Then
                dr.Read()
                Dim countTags = dr.GetValue(0)
                Return countTags
            Else
                Return 0
            End If

            myConnection.Close()

        Catch ex As Exception
            Return Nothing
        End Try
    End Function

End Class
